<?php
	class StatusController extends Controller{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Akademik";
		public $subMenu = "";
		public $title = "";
		public $loginUser;
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("akademik")){
				LoginUser::redirectToLoginPage("akademik");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_siswa_status");
			$this->subMenu = "Status Siswa";
			$data=array();
			$arrDepartment = MDepartment::model()->findAll("is_active = 1");
			$department_id = $arrDepartment != null ? $arrDepartment[0]->id : "";
			
			$arrTahunAjaran = MTahunAjaran::model()->findAll("is_active = 1 and department_id = :deptId",array(":deptId"=>$department_id));
			$tahunAjaranActive = CMTahunAjaran::getTahunAjaranActive($department_id);
			$tahun_ajaran_id = $tahunAjaranActive->id;
				
			
			$data["arrDepartment"]=$arrDepartment;
			$data["department_id"]=$department_id;
			$data["arrTahunAjaran"] =$arrTahunAjaran;
			$data["tahun_ajaran_id"] =$tahun_ajaran_id;
			
			$this->layout ="//layouts/ly_blank";
			$this->render('index',$data);
		}
		
		public function actionListDetail(){
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$conn = Yii::app()->db;
			try{
				$sql = "select a.*,b.sid as sid ,b.name as student_name,if(gender='P','Perempuan','Laki-laki') as gender,IF(status = 'PROMOTE', true, false) as promote,IF(status = 'NOT_PROMOTE', true, false) as not_promote,IF(status = 'MOVE', true, false) as move from m_student_class a inner join m_student b on a.student_id = b.id  where a.kelas_tahun_ajaran_id = :kelasTahunAjaranId and a.is_active = true";
				$params = array(
						":kelasTahunAjaranId"=>$kelas_tahun_ajaran_id,
					);
		
				$command = $conn->createCommand($sql);
				$rs =$command->query($params);
				$rw = $rs->readAll();
				
				$this->result["actionStatus"] =0;
				$this->result["actionMessage"] ="";
				$this->result["actionError"] =array();
				$this->result["model"] = $rw;
			}catch(Exception $e){
				$this->result["actionStatus"] =2;
						$this->result["actionMessage"] =$e->getMessage();
						}
			header('Content-type:application/json');
				echo CJSON::encode($this->result);
		}

		public function actionForm(){
			$op = Yii::app()->request->getParam("op","edit");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","0");				
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","description"=>"","is_active"=>"1");				
			if ($op == "add"){
				//LoginUser::getPermission("akademik_master_tingkat_add");
				//$data["op"] = "add";
				//$data["callback"] = $callback;
				//$data["pos"] = CMTingkatan::getNewPos($department_id);
			}else if($op =="edit"){
				LoginUser::getPermission("akademik_siswa_status_edit");
				$data["op"] = "edit";
				$data["callback"] = $callback;
				$id = Yii::app()->request->getParam("id","0");
				$data["id"] = $id;
			
				//$rsTingkat = MTingkatan::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				$rsData = MStudentClass::model()->findByPk($id);
				
				if ($rsData != null){ 
					print_r($rsData->student);
					echo "</br></br></br></br></br>";
					print_r($rsData->kelasTahunAjaran);
					exit;
					
					$data["student_id"] = $rsData->student_id;
					$data["student"] = $rsData->student;
					$data["kelas_tahun_ajaran"] = $rsData->kelasTahunAjaran;
					$data["notes"] = $rsData->notes;
					$data["status"] = $rsData->status;
					$data["status_notes"] = $rsData->status_notes;
					$data["is_active"] = $rsData->is_active;
					
						
				}
			}
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		} 
		
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
		public function actionedit(){
			$op = Yii::app()->request->getParam("op","");			
			$colName = Yii::app()->request->getParam("colName","");
			$value = Yii::app()->request->getParam("value","");
			$id = Yii::app()->request->getParam("id","");
			
			if ($op=="edit"){
				if (!LoginUser::getPermission("akademik_siswa_status_edit",null,false)){
					$this->result["actionStatus"] = 1;
					$this->result["actionMessage"] = Yii::t("dialog", "Anda tidak memiliki ijin untuk mengubah data!");
					header('Content-type:application/json');
					echo CJSON::encode($this->result);					
				}
				
				$studentClass = MStudentClass::model()->findByPk($id);
				if ($studentClass != null){
					if ($colName == "promote" || $colName == "not_promote" ||$colName == "move"){
						if ($value == "Yes"){
							$studentClass->status = strtoupper($colName);
						} else{
							$studentClass->status = null;
						}
					}else{
						if ($colName == "status_notes"){
							$studentClass->status_notes = $value;
						}
					}
					if ($studentClass->save()){
						$this->result["actionStatus"] = 0;
						$this->result["actionMessage"] = "";
						$this->result["actionError"] = "";
						$this->returnJSON($this->result);
					}else{
						$this->result["actionStatus"] = 1;
						$this->result["actionMessage"] = Yii::t("dialog", "data tidak dapat di simpan");
						$this->result["actionError"] = $studentClass->getErrors();
						$this->returnJSON($this->result);
					}
				} else{
					$this->result["actionStatus"] = 1;
					$this->result["actionMessage"] = Yii::t("dialog", "data tidak valid ");
					$this->returnJSON($this->result);
				}
				
				
				//$studentClassId = Yii::app()->request->getParam("student_class_id","");
				$kelasTahunAjaranIdTujuan = Yii::app()->request->getParam("kelas_tahun_ajaran_id_tujuan","");
				try{
					if ($studentClassId == "" or $kelasTahunAjaranIdTujuan ==""){
						$this->result["actionStatus"] = 1;
						$this->result["actionMessage"] = Yii::t("dialog", "Siswa atau kelas tidak valid ");
					}else{
						$studentClass = MStudentClass::model()->findByPk($studentClassId);
						$kelasTahunAjaranTujuan = MKelasTahunAjaran::model()->findByPk($kelasTahunAjaranIdTujuan);
												
						if ( $studentClass["kelas_tahun_ajaran_id"] == $kelasTahunAjaranIdTujuan){
							$this->result["actionStatus"] = 1;
							$this->result["actionMessage"] = Yii::t("dialog", "Kelas asal dan tujuan sama.");
						}else{
							if ($studentClass["is_active"] == 0){
								$this->result["actionStatus"] = 1;
								$this->result["actionMessage"] = Yii::t("dialog", "Siswa atau kelas tidak valid ");
								$this->returnJSON($this->result);
							}
							
							$transaction = Yii::app()->db->beginTransaction();
							try{
								// set student_class asal to is_active = false;
								$studentClass["is_active"] = 0;
								$studentClass["delete_date"] = date("Y-m-d H:i:s");
								$studentClass["last_user_id"] = $this->loginUser["id"];
								if ($studentClass->save()){
									// add student_class with Kelas Tahun ajaran Tujuan
									$newStudentClass = MStudentClass::model()->find("student_id =:studentId and kelas_tahun_ajaran_id =:kelasTahunAjaranId",
											array(":studentId"=>$studentClass["student_id"],":kelasTahunAjaranId" =>$kelasTahunAjaranTujuan["id"]));
									if ($newStudentClass == null){
										$newStudentClass = new MStudentClass();
										$newStudentClass->student_id = $studentClass["student_id"];
										$newStudentClass->kelas_tahun_ajaran_id = $kelasTahunAjaranTujuan["id"];											
										$newStudentClass->input_date = date("Y-m-d H:i:s");
										$newStudentClass->monthly_fee = $kelasTahunAjaranTujuan->monthly_fee;
										$newStudentClass["input_date"] = date("Y-m-d H:i:s");										
									}else{
										$newStudentClass["last_update_date"] = date("Y-m-d H:i:s");
									}
									$newStudentClass->is_active = 1;
									$newStudentClass->last_user_id = $this->loginUser["id"];
									
									if ($newStudentClass->save()){
										$this->result["actionStatus"] = 0;
										$this->result["id"] = $newStudentClass->id;
									}else{
										$this->result["actionStatus"] = 2;
										$this->result["errorMessage"] = $studentClass->getErrors();
									}									
								}else{
									$this->result["actionStatus"] = 2;
									$this->result["errorMessage"] = $studentClass->getErrors();
								}
								
								if ($this->result["actionStatus"] == 0){
									$transaction->commit();
								}
								
							}catch (Exception $e){
								$transaction->rollback();
							}
						}						
					}
				}catch (Exception $e){
					$this->result["actionStatus"] = 2;
					$this->result["errorMessage"] = $e->getMessage();
				}
				$this->returnJSON($this->result);			
			}
		}			
	
		public function actionTrialToActive(){
			LoginUser::getPermission("akademik_siswa_activate_trial");
			$this->subMenu = "Aktivasi Trial Siswa";
			
			$status = Yii::app()->request->getParam("status_siswa","TRIAL");
			$department_id = Yii::app()->request->getParam("department_id","");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			
			if ($status == ""){
				$arrData = MStudent::model()->findAll("status_siswa in ('TRIAL','ACTIVE')");
			}else{
				$arrData = MStudent::model()->findAll("status_siswa in (:status)",array(":status"=>$status));
			}
			
			print_r($arrData);
			exit;
			$this->layout ="//layouts/ly_blank";
			$this->render('idxActivateTrial',$data);
		}		
	}
?>